package com.bw.smartpark

import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bw.common.mvvm.BaseViewActivity
import com.bw.smartpark.databinding.ActivityWelcomeBinding
import com.therouter.TheRouter
import com.therouter.router.Route
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.indicator.CircleIndicator

@Route(path = "app/WelcomeActivity")
class WelcomeActivity : BaseViewActivity<ActivityWelcomeBinding>() {

    //轮播图照片
    private val ImageBanner = listOf(
        R.drawable.banner_img3,
        R.drawable.banner_img3,
        R.drawable.banner_img3
    )

    override fun initData() {
        initBanner()
        binding.welcomeBtn.setOnClickListener {
            TheRouterMain()
        }
        binding.welcomeSkip.setOnClickListener {
            TheRouterMain()
        }
    }



    //轮播图
    private fun initBanner() {
        //简洁高效，内置指示器和自动轮播
        binding.welcomeBanner.apply {
            setAdapter(object : BannerAdapter<Int, BannerViewHolder>(ImageBanner) {
                override fun onCreateHolder(parent: ViewGroup?, viewType: Int): BannerViewHolder {
                    return BannerViewHolder(
                        ImageView(parent?.context).apply {
                            layoutParams = ViewGroup.LayoutParams(
                                ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.MATCH_PARENT
                            )
                            scaleType = ImageView.ScaleType.CENTER_CROP
                        }
                    )
                }

                override fun onBindView(
                    holder: BannerViewHolder?,
                    data: Int?,
                    position: Int,
                    size: Int
                ) {
                    if (holder != null) {
                        Glide.with(holder.itemView)
                            .load(data)
                            .into(holder.imageView)
                    }
                }
            })

            //轮播图指示器
            indicator = CircleIndicator(context).apply {
                setIndicatorWidth(8.dp(this@WelcomeActivity), 16.dp(this@WelcomeActivity))
                setIndicatorSpace(4.dp(this@WelcomeActivity))
                setIndicatorSelectedColor(Color.BLUE)  // 直接使用颜色常量
                setIndicatorNormalColor(Color.GRAY)
            }
            //轮播图的生命周期与 Activity/Fragment 绑定 => 防止轮播图未停止导致内存泄漏
            addBannerLifecycleObserver(this@WelcomeActivity)
        }

    }


    override fun initView() {
//        var mmkv = MMKV.defaultMMKV()
//        if (mmkv.decodeBool("first_welcome",false)){
//            TheRouter.build("app/MainActivity").navigation()
//            finish()
//        }
    }

    /*  ViewHolder类 内部类 {
        需要访问外部类资源  直接访问
        简化代码
    }
    */
   inner class BannerViewHolder (view:View):RecyclerView.ViewHolder(view){
       val imageView:ImageView =view as ImageView
   }

    // 扩展函数
    fun Int.dp(context: Context): Int = (this * Resources.getSystem().displayMetrics.density).toInt()

    private fun TheRouterMain() {
      TheRouter.build("app/MainActivity").navigation()
    }

}


